package com.huilian.tool.common.utils;

import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;
import org.springframework.http.MediaType;
import org.springframework.stereotype.Service;
import org.springframework.web.client.RestTemplate;

import javax.annotation.Resource;

/**
 * @Author 少凡
 * @Date 2022-06-27 17:03
 * version 1.0
 */
@Service
public class RestTemplateUtil {

    @Resource
    private RestTemplate restTemplate;

    public <T> T unifyPost(String url, Object object, Class<T> responseType) {
        HttpHeaders headers = new HttpHeaders();
        headers.setContentType(MediaType.APPLICATION_JSON);
        headers.setConnection("close");
        HttpEntity entity = new HttpEntity<>(object, headers);
        return restTemplate.postForObject(url, entity, responseType);
    }

    public <T> T unifyGet(String url, Class<T> responseType) {
        return restTemplate.getForObject(url, responseType);
    }

}
